package com.anmong.cloud.cloudauth.controller;

import com.alibaba.fastjson2.JSONObject;
import com.anmong.cloud.cloudapi.base.BasePageDTO;
import com.anmong.cloud.cloudapi.dto.TestUpdateDTO;
import com.anmong.cloud.cloudapi.entity.SysUser;
import com.anmong.cloud.cloudapi.mapper.SysUserMapper;
import com.anmong.cloud.cloudcommon.exception.RpcInternalException;
import com.anmong.cloud.cloudcommon.util.RedisUtil;
import com.anmong.cloud.cloudcommon.exception.CommonException;
import com.anmong.cloud.cloudcommon.response.R;
import com.anmong.cloud.cloudcommon.util.IDUtil;
import com.anmong.cloud.cloudcommon.util.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("auth")
@Tag(name = "认证接口", description = "认证接口")
public class AuthController {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("id")
    @Operation(summary = "测试获取id", description = "测试获取id")
    public String getId(){
        return String.valueOf(IDUtil.get());
    }

    @GetMapping("insert")
    @Operation(summary = "测试插入数据库", description = "测试插入数据库")
    public SysUser insert(){
        SysUser sysUser = new SysUser();
        sysUser.setId(IDUtil.get());
        sysUser.setUsername("123");
        sysUser.setNickname("123");
        sysUser.setPassword("123");
        sysUser.setCreateUser(IDUtil.get());
        sysUser.setUpdateUser(IDUtil.get());
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setUpdateTime(LocalDateTime.now());
        sysUserMapper.insert(sysUser);
        return sysUser;
    }

    @PutMapping("update")
    @Operation(summary = "测试更新库", description = "测试插入数据库")
    public R update(@RequestBody @Valid TestUpdateDTO dto){
        SysUser sysUser = sysUserMapper.selectById(dto.getId());
        if (sysUser == null){
            throw new RpcInternalException("用户不存在!");
        }
        sysUser.setUpdateTime(dto.getUpdateTime());
        sysUserMapper.updateById(sysUser);
        return R.collectionItem(sysUser);
    }

    @GetMapping("page")
    @Operation(summary = "测试分页", description = "测试插入数据库")
    public R page(BasePageDTO dto){
        IPage<SysUser> page = sysUserMapper.selectPage(new Page<SysUser>(dto.getPage(),dto.getSize())
                ,new QueryWrapper<SysUser>()
        );

        return R.page(new PageUtils(page));
    }

    @GetMapping("user/{id}")
    @Operation(summary = "根据id查询", description = "测试插入数据库")
    public R findById(@PathVariable String id){
        return R.collectionItem(sysUserMapper.selectById(id));
    }

    @PostMapping("addToRedis")
    @Operation(summary = "插入", description = "测试插入数据库")
    public R addToRedis(Long id){
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            throw new CommonException("用户不存在!");
        }
        redisUtil.set(id.toString(), JSONObject.toJSONString(sysUser));
        return R.ok();
    }

    @GetMapping("list")
    @Operation(summary = "测试列表", description = "测试插入数据库")
    public R list(){
        List<SysUser> page = sysUserMapper.selectList(new QueryWrapper<SysUser>()
        );
        return R.collection(page);
    }

    @GetMapping("empty")
    @Operation(summary = "测试列表", description = "测试插入数据库")
    public R empty(){
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }

}
